package com.vf.admin.miaodong.controller.pay;

import cn.hutool.core.util.ObjectUtil;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AlipayConfigs;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.miaodong.service.AfterAlipayConfigsService;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>Title : </p>
 * <p>Description : 阿里云支付配置</p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows10</p>
 *
 * @Author : NemoMeng
 * @Date 2023/6/27 10:24
 */
@Controller
@RequestMapping("/after/aliPay")
public class AfterAliPayController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AfterAlipayConfigsService afterAlipayConfigsService;


    /**
     * 列表
     *
     * @param splitPage
     * @param status
     * @return
     */
    @RequestMapping("/aliPayConfigList")
    public String aliPayConfigList(SplitPageBean splitPage, Integer status) {
        //构建参数
        Map<String, Object> map = new HashMap<>();
        map.put("status", status);
        Map<Integer, String> statusMap = Constants.getstatusMap();
        List<AlipayConfigs> list = afterAlipayConfigsService.list(map, splitPage);
        request.setAttribute("list", list); //列表
        request.setAttribute("splitPage", splitPage);  //分页参数
        request.setAttribute("statusMap", statusMap);
        request.setAttribute("status", status);
        return this.sucess("/web/after/pay/aliPayConfigList");

    }

    /**
     * 保存或是修改
     *
     * @param params
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(AlipayConfigs params) {
        try {
            if (ObjectUtil.isNull(params.getStatus())) {
                params.setStatus(2);
            }
            afterAlipayConfigsService.saveOrUpdate(params);
            return sucessJSON("成功");
        } catch (Exception e) {
            return errorJSON("系统繁忙");
        }
    }


    /**
     * 详情
     */
    @RequestMapping("/detail")
    public String detail(Integer id) {

        AlipayConfigs alipayConfigs = afterAlipayConfigsService.get(id);
        request.setAttribute("alipayConfigs", alipayConfigs);
        //显示状态
        Map<Integer, String> statusMap = Constants.getstatusMap();
        request.setAttribute("statusMap", statusMap);
        return this.sucess("/web/after/pay/aliPayEdit");

    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @ResponseBody
    public Result del(Integer id) {
        try {
            if (StringEx.isNull(id)) {
                return this.errorJSON("未获取到要删除的信息");
            }
            afterAlipayConfigsService.del(id);
            return this.sucessJSON("删除成功");
        } catch (Exception e) {
            logger.error("删除礼物数量时异常", e);
            return this.errorJSON("系统繁忙");
        }
    }
}
